package team.seekers.hula.island.postComment.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import team.seekers.hula.common.utils.BaseConvertUtils;
import team.seekers.hula.island.post.service.IslandPostService;
import team.seekers.hula.island.postComment.entity.dao.IslandPostCommentReplyDO;
import team.seekers.hula.island.postComment.entity.dto.*;
import team.seekers.hula.island.postComment.entity.vo.PostCommentInfoVO;
import team.seekers.hula.island.postComment.entity.vo.PostCommentReplyListVO;
import team.seekers.hula.record.wind.service.WindService;
import team.seekers.hula.auth.service.MindUtils;
import team.seekers.hula.common.entity.PageResult;
import team.seekers.hula.island.postComment.dao.IslandPostCommentDao;
import team.seekers.hula.island.postComment.entity.dao.IslandPostCommentDO;
import team.seekers.hula.island.postComment.entity.vo.PostCommentListVO;
import team.seekers.hula.user.user.service.UserService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ekin
 * @Description GOOD
 * @mood happy
 */
@Service
public class IslandPostCommentService extends IslandPostCommentDao{

    @Autowired
    private IslandPostCommentDao islandPostCommentDao;

    @Autowired
    private IslandPostCommentReplyService islandPostCommentReplyService;

    @Resource
    private UserService userService;

    @Autowired
    @Lazy
    private IslandPostService islandPostService;

    @Autowired
    private WindService windService;

    public PostCommentListVO postComment(PostCommentDTO postCommentDTO) {
        IslandPostCommentDO islandPostCommentDO = new IslandPostCommentDO();
        BeanUtils.copyProperties(postCommentDTO,islandPostCommentDO);

        islandPostCommentDao.save(islandPostCommentDO);

        // 新增帖子评论数
        islandPostService.addCommentCount(islandPostCommentDO.getPostId());

        PostCommentListVO postCommentListVO = new PostCommentListVO();
        BeanUtils.copyProperties(islandPostCommentDO, postCommentListVO);
        postCommentListVO.setUserInfo(userService.getFollowInfoVO(MindUtils.getUserId(),MindUtils.getUserId()));

        return postCommentListVO;
    }

    public PostCommentReplyListVO replyComment(PostCommentReplyDTO replyDTO) {
        // 查询信息
        replyDTO.setToNickname(userService.getBasicInfoVO(replyDTO.getToId()).getNickname());
        replyDTO.setFloor(islandPostCommentReplyService
                .lambdaQuery()
                .eq(IslandPostCommentReplyDO::getPostCommentId,replyDTO.getPostCommentId())
                .count());

        IslandPostCommentReplyDO replyDO = new IslandPostCommentReplyDO();
        BeanUtils.copyProperties(replyDTO,replyDO);
        islandPostCommentReplyService.save(replyDO);
        return islandPostCommentReplyService.getListVO(replyDO.getId());
    }

    public List<PostCommentListVO> getMyComment(PostCommentListDTO postCommentListDTO) {
        List<IslandPostCommentDO> comments = islandPostCommentDao
                .list(new QueryWrapper<IslandPostCommentDO>()
                        .eq("user_id", postCommentListDTO.getUserId())
                        .eq("post_id", postCommentListDTO.getPostId()));

        if (null == comments){
            return new ArrayList<>();
        }

        ArrayList<PostCommentListVO> postCommentListVOS = new ArrayList<>();
        for (IslandPostCommentDO comment : comments) {
            PostCommentListVO postCommentListVO = new PostCommentListVO();
            BeanUtils.copyProperties(comment, postCommentListVO);
            postCommentListVO.setUserInfo(userService.getFollowInfoVO(MindUtils.getUserId(),MindUtils.getUserId()));
            postCommentListVOS.add(postCommentListVO);
        }


        return postCommentListVOS;
    }

    public List<PostCommentListVO> listAllComment(PostCommentListDTO postCommentListDTO) {
        List<IslandPostCommentDO> list = islandPostCommentDao
                .list(new QueryWrapper<IslandPostCommentDO>().eq("post_id", postCommentListDTO.getPostId()));

        ArrayList<PostCommentListVO> postCommentListVOS = new ArrayList<>();
        for (IslandPostCommentDO comment : list) {
            PostCommentListVO postCommentListVO = new PostCommentListVO();
            BeanUtils.copyProperties(comment, postCommentListVO);
            postCommentListVO.setUserInfo(userService.getFollowInfoVO(comment.getUserId(), MindUtils.getUserId()));
            postCommentListVO.setIsLike(BaseConvertUtils.stringToList(comment.getLikeUserIds()).contains(MindUtils.getUserId()));

            // 填充几个评论
            postCommentListVO.setReplies(islandPostCommentReplyService.listThree(comment.getId()));

            postCommentListVOS.add(postCommentListVO);
        }
        return postCommentListVOS;
    }

    public PageResult<PostCommentListVO> pageComment(PostCommentQueryDTO queryDTO) {
        LambdaQueryChainWrapper<IslandPostCommentDO> lambdaQuery = islandPostCommentDao.lambdaQuery();

        if(!StringUtils.isEmpty(queryDTO.getPostId())){
            lambdaQuery.eq(IslandPostCommentDO::getPostId, queryDTO.getPostId());
        }

        if(!StringUtils.isEmpty(queryDTO.getUserId())){
            lambdaQuery.eq(IslandPostCommentDO::getUserId, queryDTO.getUserId());
        }

        if(!StringUtils.isEmpty(queryDTO.getIslandId())){
            lambdaQuery.eq(IslandPostCommentDO::getIslandId, queryDTO.getIslandId());
        }

        if(!StringUtils.isEmpty(queryDTO.getContent())){
            lambdaQuery.like(IslandPostCommentDO::getContent,queryDTO.getContent());
        }

        if(!StringUtils.isEmpty(queryDTO.getId())){
            lambdaQuery.like(IslandPostCommentDO::getId,queryDTO.getId());
        }

        lambdaQuery.orderByDesc(IslandPostCommentDO::getGmtCreate);

        Page<IslandPostCommentDO> page = lambdaQuery.page(new Page<>(queryDTO.getPageNow(), queryDTO.getPageSize()));
        long total = page.getTotal();
        List<IslandPostCommentDO> records = page.getRecords();
        if(records == null){
            return new PageResult<>();
        }
        ArrayList<PostCommentListVO> postCommentListVOS = new ArrayList<>();
        for (IslandPostCommentDO comment : records) {
            PostCommentListVO postCommentListVO = new PostCommentListVO();
            BeanUtils.copyProperties(comment, postCommentListVO);
            postCommentListVO.setUserInfo(userService.getFollowInfoVO(comment.getUserId(), MindUtils.getUserId()));
            postCommentListVO.setIsMine(MindUtils.getUserId().equals(comment.getUserId()));

            List<String> userIds = BaseConvertUtils.stringToList(comment.getLikeUserIds());
            postCommentListVO.setIsLike(userIds.contains(MindUtils.getUserId()));
            postCommentListVO.setLikeCount(userIds.size());

            // 填充几个评论
            postCommentListVO.setReplies(islandPostCommentReplyService.listThree(comment.getId()));

            postCommentListVOS.add(postCommentListVO);
        }
        return new PageResult<>(total,postCommentListVOS);
    }

    public void updatePostComment(PostCommentUpdateDTO updateDTO) {
        IslandPostCommentDO islandPostCommentDO = islandPostCommentDao.getById(updateDTO.getId());
        BeanUtils.copyProperties(updateDTO, islandPostCommentDO);
        islandPostCommentDao.updateById(islandPostCommentDO);
    }

    public void removeByPostId(String id) {
        islandPostCommentDao.lambdaUpdate().eq(IslandPostCommentDO::getPostId,id).remove();
    }

    public void likeOrCancel(String commentId,String userId) {
        IslandPostCommentDO postCommentDO = islandPostCommentDao.lambdaQuery().select(IslandPostCommentDO::getId,
                IslandPostCommentDO::getLikeUserIds).eq(IslandPostCommentDO::getId, commentId).one();
        List<String> userIds = BaseConvertUtils.stringToList(postCommentDO.getLikeUserIds());
        if(userIds.contains(userId)){
            userIds.remove(userId);
        }else{
            userIds.add(userId);
        }
        postCommentDO.setLikeUserIds(BaseConvertUtils.listToString(userIds));
        updateById(postCommentDO);
    }

    public PostCommentInfoVO info(String id) {
        IslandPostCommentDO postCommentDO = getById(id);
        PostCommentInfoVO infoVO = new PostCommentInfoVO();
        BeanUtils.copyProperties(postCommentDO,infoVO);
        infoVO.setUserInfo(userService.getFollowInfoVO(postCommentDO.getUserId(),MindUtils.getUserId()));

        infoVO.setIsMine(MindUtils.getUserId().equals(postCommentDO.getUserId()));

        List<String> userIds = BaseConvertUtils.stringToList(postCommentDO.getLikeUserIds());
        infoVO.setIsLike(userIds.contains(MindUtils.getUserId()));
        infoVO.setLikeCount(userIds.size());

        return infoVO;
    }

    public void delete(String id) {
        // 删除所有回复
        islandPostCommentReplyService.removeByCommentId(id);

        removeById(id);
    }

    public Integer countByPostId(String postId) {
        return lambdaQuery().eq(IslandPostCommentDO::getPostId,postId).count();
    }
}
